﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.manage
{
    public partial class area4 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Literal_map.Text = RuleCode.Util.getmapurl();
                InitMap();
                bind1();
                bind2();
                bind3();
            }
        }
        /// <summary>
        /// 县
        /// </summary>
        private void bind1()
        {
            string sheng = zfunction.Province;
            RuleCode.Table.县 x = RuleCode.Table.县.Find(" 省编码 = '" + sheng + "' ");
            DropDownList1.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList1.Items.Add(item["县编码"].ToString());
            }
            //DropDownList1.DataBind();
         
        }
        /// <summary>
        /// 区
        /// </summary>
        private void bind2()
        {
            string xian = DropDownList1.Text;
            RuleCode.Table.区 x = RuleCode.Table.区.Find(" 县编码 = '" + xian + "' ");
            DropDownList2.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList2.Items.Add(item["区编码"].ToString());

            }
            if (!IsPostBack && x.Count == 0)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                    //    + "" + "39.917043746896674,'" + x.区编码.ToString() + "');</script>";
                    string provice = zfunction.Province;
                    string county = DropDownList1.SelectedValue;
                    string query = provice + "," + county;
                     ScriptTag = "showAddress('" + query + "');";

                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区编码.ToString() + "');</script>";
                }
                ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
            }

        }

        /// <summary>
        /// 区域表格
        /// </summary>
        private void bind3()
        {
            RuleCode.Table.区域 x = RuleCode.Table.区域.Find(" 区编码 = '" + DropDownList2.Text + "' "
                + " and 县编码 = '" + DropDownList1.Text + "' ");
            this.GridView1.DataSource = x.MyTable;
            this.GridView1.DataBind();
            if (!IsPostBack)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + x.区编码.ToString() + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区编码.ToString() + "');</script>";
                }
                ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);

            }
            else
            {
                RuleCode.Table.区 x1 = RuleCode.Table.区.Find(" 区编码 = '" + DropDownList2.Text + "' "
                + " and 县编码 = '" + DropDownList1.Text + "' ");
                string ScriptTag =
                     "<script type=\"text/javascript\">markpos2(" + x1.taobaolong.ToString() + ","
                     + x1.taobaolat.ToString() + ",'" + x1.区编码.ToString() + "');</script>";

                ScriptManager.RegisterStartupScript(
                this.UpdatePanel1, this.UpdatePanel1.GetType(), x1.taobaolong.ToString()+x1.taobaolat.ToString(), ScriptTag, false);
            }
        }


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind2();
            bind3();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            bind3();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            区域 uc = 区域.Find(" recid = '" + id + "'", "RECID");
            uc.Delete();
            bind1();
        }
        private void InitMap()
        {
            string ScriptTag = "";


            ScriptTag =
                "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                + "" + "39.917043746896674,'" + "2222" + "');</script>";

            ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
            区域 uc = 区域.Find(" recid = '" + id + "'", "RECID");

            TextBox1.Text = uc.区域编码.ToString();
            TextBox_long.Text = uc.taobaolong.ToString();
            TextBox_lat.Text = uc.taobaolat.ToString();
            Label_recid.Text = uc.RECID.ToString();
            string ScriptTag = "";
            if (uc.taobaolong.Value == 0 || uc.taobaolat.Value == 0)
            {
                ScriptTag =
                    "markpos2(" + "" + "116.39679908752441,"
                    + "39.917043746896674" + ",'" + uc.区域编码.ToString() + "')";
            }
            else
            {
                ScriptTag =
                    "markpos2(" + uc.taobaolong.ToString() + ","
                    + uc.taobaolat.ToString() + ",'" + uc.区域编码.ToString() + "')";
            }
            ScriptManager.RegisterStartupScript(
                this.UpdatePanel1, this.UpdatePanel1.GetType(), "bodyevent3", ScriptTag, true);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList1.Text != "" && DropDownList2.Text != "")
            {
                区域 uc = 区域.Find(" 1=2 ");
                uc.New();
                uc.区编码.Value = DropDownList2.Text;
                uc.县编码.Value = DropDownList1.Text;
                uc.区域编码.Value = TextBox1.Text;
                uc.taobaolong.Value = zfunction.str2double(TextBox_long.Text);
                uc.taobaolat.Value = zfunction.str2double(TextBox_lat.Text);
                uc.Insert();
                bind3();
            }
            else
            {
                MessageBox.Show(this.UpdatePanel1, "操作不成功.");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            区域 x = 区域.Find(" recid = '" + Label_recid.Text + "'", "RECID");
            x.区域编码.Value = TextBox1.Text;
            x.taobaolong.Value = zfunction.str2double(TextBox_long.Text);
            x.taobaolat.Value = zfunction.str2double(TextBox_lat.Text);
            x.Modify();
            bind3();
        }


    }
}
